package com.itboye.ihomebank;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.controller.LoginController;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.home.ActivitySwitchRoles;
import com.itboye.ihomebank.login.LoginActivity;
import com.itboye.ihomebank.me.ActivityFaBuHouse;
import com.itboye.ihomebank.me.ActivityGuanJia;
import com.itboye.ihomebank.me.ActivityInviteFriends;
import com.itboye.ihomebank.me.ActivityMyData;
import com.itboye.ihomebank.me.ActivitySetting;
import com.itboye.ihomebank.me.ActivityWoDeQiuZu;
import com.itboye.ihomebank.me.WodeFangyuanActivity;
import com.itboye.ihomebank.me.key.ActivityAdministrationKey;
import com.itboye.ihomebank.me.key.ActivityMyKey;
import com.itboye.ihomebank.myzhujia.ActivityMyWallte;
import com.itboye.ihomebank.myzhujia.qianbao.ActivityZhiFuPassSheZhi;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;
import com.itboye.ihomebank.web.WebActivity;

/**
 * 我的
 * 
 * @author
 * 
 */
public class MeFragment extends BaseFragment implements OnClickListener,
		OnRefreshListener, Observer {
	LinearLayout person_qiehuan;// 切换角色
	ImageView person_shezhi;// 设置
	ImageView queryPersonData;
	RoundImageView roundImage;
	TextView name;
	TextView phone_num;
	RelativeLayout lin_mywallet;// 钱包
	RelativeLayout lin_mycollection;// 收藏
	RelativeLayout lin_myappointment;// 预约
	RelativeLayout lin_mysign;// 签约
	RelativeLayout mylifemanager_rel;// 生活管家
	RelativeLayout invitefriend_rel;// 邀请好友
	RelativeLayout myassess_rel;// 我的租房
	LinearLayout head;// 头像（整块）
	TextView geren_tv01;
	String role = null;
	private String identity;
	private String broker;
	RelativeLayout RelativeLayout_out;
	SwipeRefreshLayout main_srl;

	@Override
	public int initView() {
		return R.layout.activity_person_centre;
	}

	@Override
	public void onMyActivityCreated() {

	}

	@Override
	public void onResume() {
		super.onResume();
		read();
	}

	/**
	 * 加入数据
	 */
	private void read() {
		String nikeName = (String) SPUtils.get(getActivity(), null,
				SPContants.NICK, "");
		name.setText("" + nikeName);

		String mobile = (String) SPUtils.get(getActivity(), null,
				SPContants.MOBILE, "");

		if (!mobile.equals("")) {
			try {
				String hintphone = mobile.substring(0, 3) + "****"
						+ mobile.substring(7, 11);
				phone_num.setText(hintphone);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			phone_num.setText("");
		}
		System.out.println(">>>>>>head"
				+ (String) SPUtils.get(getActivity(), null,
						SPContants.AVATAR_URL, ""));
		String resultData = ""
				+ SPUtils.get(getActivity(), null, SPContants.AVATAR_URL, "");
		if (resultData != null) {
			if (resultData.equals("")) {
				roundImage.setImageResource(R.drawable.default_head);
				// roundImage.setBackgroundResource(R.drawable.default_head);
			} else {
				if (resultData.startsWith("http")) {
					XImageLoader.load(resultData, roundImage);
				} else {
					if (resultData.equals("0")) {
						roundImage.setImageResource(R.drawable.default_head);
					} else {
						XImageLoader.load(NetPublicConstant.IMAGE_URL
								+ resultData, roundImage);
					}
				}
			}
		} else {
			roundImage.setImageResource(R.drawable.default_head);
		}
		identity = SPUtils.get(getActivity(), null,
				SPContants.IdentityValidate, "") + "";
		broker = SPUtils
				.get(getActivity(), null, SPContants.BrokerValidate, "") + "";
		// if (identity.equals("") || identity.equals("")) {
		// geren_tv01.setVisibility(View.GONE);
		// geren_tv01.setText("");
		// }
		broker = SPUtils
				.get(getActivity(), null, SPContants.BrokerValidate, "") + "";
		role = SPUtils.get(getActivity(), null, SPContants.ROLE, "") + "";
		identity = SPUtils.get(getActivity(), null,
				SPContants.IdentityValidate, "") + "";
		if (!"".equals(SPUtils.get(getActivity(), null,
				SPContants.BrokerValidate, "") + "")) {
			geren_tv01.setText("游客");
		} else {
			geren_tv01.setText("未登录");
		}
		if (identity != null) {
			if (identity.equals("1")) {
				geren_tv01.setText("已认证");
			} else {
				geren_tv01.setText("未认证");
			}
		}
		// if (broker != null) {
		// if (broker.equals("1")) {
		// geren_tv01.setText("经纪人");
		// }
		// }
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		userPresenter = new UserPresenter(this);
		main_srl.setOnRefreshListener(this);
		main_srl.setColorSchemeResources(android.R.color.holo_blue_bright,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
		IntentFilter intentFilterAvart = new IntentFilter(
				ActivityMyData.AVARTACTION);// 当收到头像修改广播时修改头像
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart);

		IntentFilter intentFilterExit = new IntentFilter(
				ActivitySetting.EXITCHANGE);// 当收到退出登录广播的时候执行
		getActivity()
				.registerReceiver(avartBroadcastReceiver, intentFilterExit);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.person_qiehuan:// 切换角色

			if ((SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "")
					.equals("")) {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			} else {
				if ((SPUtils.get(getActivity(), null,
						SPContants.IdentityValidate, "") + "")// 未认证用户
						.equals("0")
						|| (SPUtils.get(getActivity(), null,
								SPContants.IdentityValidate, "") + "")// 未认证用户
								.equals("")) {
					ByAlert.alert("还没有实名认证哦!");
				} else if ((SPUtils.get(getActivity(), null,
						SPContants.IdentityValidate, "") + "")// 未认证用户
						.equals("2")) {
					ByAlert.alert("实名认证还在审核中哦!");
				} else {
					intent = new Intent(getActivity(),
							ActivitySwitchRoles.class);
					startActivity(intent);
				}
			}
			break;
		case R.id.person_shezhi:// 设置
			intent = new Intent(getActivity(), ActivitySetting.class);
			startActivity(intent);
			break;
		case R.id.queryPersonData:
			LoginController.onMyPersonalCenter(getActivity(), null);
			break;
		case R.id.lin_mywallet:// 钱包
			if ((SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "")
					.equals("")) {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			} else {
				if ((SPUtils.get(getActivity(), null,
						SPContants.IdentityValidate, "") + "")// 未认证用户
						.equals("0")
						|| (SPUtils.get(getActivity(), null,
								SPContants.IdentityValidate, "") + "")// 未认证用户
								.equals("")) {
					ByAlert.alert("还没有实名认证哦!");
				} else if ((SPUtils.get(getActivity(), null,
						SPContants.IdentityValidate, "") + "")// 未认证用户
						.equals("2")) {
					ByAlert.alert("实名认证还在审核中哦!");
				} else if ((SPUtils.get(getActivity(), null,
						SPContants.WalletPass, "") + "")// 未认证用户
						.equals("")) {
					intent = new Intent(getActivity(),
							ActivityZhiFuPassSheZhi.class);
					startActivity(intent);
				} else if ((SPUtils.get(getActivity(), null,
						SPContants.BrokerValidate, "") + "")// 为经纪人只能查看收支明细
						.equals("1")) {
					intent = new Intent(getActivity(), WebActivity.class);
					intent.putExtra(
							"url",
							NetPublicConstant.WEB_URL
									+ "/wallet/balance?uid="
									+ (SPUtils.get(getActivity(), null,
											SPContants.USER_ID, "") + "")
									+ "&psw="
									+ (SPUtils.get(getActivity(), null,
											SPContants.PASSWORD, "") + ""));
					intent.putExtra("title", "收支明细");
					startActivity(intent);
				} else if ((SPUtils.get(getActivity(), null,
						SPContants.IdentityValidate, "") + "")// 为房东可以查看自己的钱包
						.equals("1")) {
					intent = new Intent(getActivity(), ActivityMyWallte.class);
					startActivity(intent);
				}
			}
			// LoginController.onMyMoneyClick(getActivity(), null);
			break;
		case R.id.lin_mycollection:// 收藏
			LoginController.onMyCollectionClick(getActivity(), null);
			break;
		case R.id.lin_myappointment:// 预约
			LoginController.onMyAppointmentClick(getActivity(), null);
			break;
		case R.id.lin_mysign:// 签约
			LoginController.onMySignClick(getActivity(), null);
			break;
		case R.id.mylifemanager_rel:// 生活管家
			if ((SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "")
					.equals("")) {
				intent = new Intent(getActivity(), LoginActivity.class);
			} else {
				intent = new Intent(getActivity(), ActivityGuanJia.class);
			}

			startActivity(intent);
			break;
		// case R.id.myintelligent_lock_rel:// 智能锁
		// if ((SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "")
		// .equals("")) {
		// intent = new Intent(getActivity(), LoginActivity.class);
		// } else {
		// intent = new Intent(getActivity(), ActivityMyKey.class);
		// }
		// startActivity(intent);
		// break;
		case R.id.myassess_rel:// 我的租房
			if ((SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "")
					.equals("")) {
				intent = new Intent(getActivity(), LoginActivity.class);
			} else {
				intent = new Intent(getActivity(), ActivityWoDeQiuZu.class);
			}
			startActivity(intent);
			break;
		case R.id.invitefriend_rel:// 邀请好友
			intent = new Intent(getActivity(), ActivityInviteFriends.class);
			startActivity(intent);
			break;
		case R.id.geren_tv01:
			// geren_tv01.setAlpha(0.8f);
			// geren_tv02.setAlpha(0.4f);
			// geren_tv03.setAlpha(0.4f);
			break;
		case R.id.RelativeLayout_out:
			LoginController.onMyPersonalCenter(getActivity(), null);
			break;
		default:
			break;
		}
	}

	/**
	 * 广播 当收到头像更改 更换头像
	 */
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			read();
		};
	};
	private UserPresenter userPresenter;

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		String uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "")
				+ "";
		if (uid.equals("")) {

		} else {
			userPresenter.getPersonData(uid);
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.persondata_success) {
				PersonDataBean personDataBean = (PersonDataBean) entity
						.getData();
				if (personDataBean != null) {
					putSpData(personDataBean);
				}
			} else if (entity.getEventType() == UserPresenter.persondata_success) {

			}
			main_srl.setRefreshing(false);
		}
	}

	private void putSpData(PersonDataBean bean) {
		// TODO Auto-generated method stub
		if (bean.getUsername() != null)
			SPUtils.put(getActivity(), null, SPContants.USERNAME,
					bean.getUsername());
		if (bean.getMobile() != null)
			SPUtils.put(getActivity(), null, SPContants.MOBILE,
					bean.getMobile());
		if (bean.getMobile() != null) {
			if (bean.getMobile().equals("")) {
				SPUtils.put(getActivity(), null, SPContants.IS_BIND_PHONE,
						false);
			} else {
				SPUtils.put(getActivity(), null, SPContants.IS_BIND_PHONE, true);
			}
		} else {
			SPUtils.put(getActivity(), null, SPContants.IS_BIND_PHONE, false);
		}
		SPUtils.put(getActivity(), null, SPContants.IS_LOGINED, true);
		if (bean.getEmail() != null)
			SPUtils.put(getActivity(), null, SPContants.EMAIL, bean.getEmail());
		if (bean.getNickname() != null)
			SPUtils.put(getActivity(), null, SPContants.NICK,
					bean.getNickname());
		if (bean.getPassword() != null)
			SPUtils.put(getActivity(), null, SPContants.PASSWORD,
					bean.getPassword());
		if (bean.getUid() != null)
			SPUtils.put(getActivity(), null, SPContants.USER_ID, bean.getUid());
		if (bean.getHead() != null)
			SPUtils.put(getActivity(), null, SPContants.AVATAR_URL,
					bean.getHead());
		if (bean.getSex() != null)
			SPUtils.put(getActivity(), null, SPContants.SEX, bean.getSex());// 性别
		if (bean.getIdentityValidate() != null)
			SPUtils.put(getActivity(), null, SPContants.IdentityValidate,// '实名身份认证【0未认证,1已认证,2待审核】'
					bean.getIdentityValidate());
		if (bean.getBrokerValidate() != null)
			SPUtils.put(getActivity(), null, SPContants.BrokerValidate,
					bean.getBrokerValidate());// 经纪人认证【0未认证,1已认证,2待审核】
		if (bean.getScienerUsername() != null)
			SPUtils.put(getActivity(), null, SPContants.ScienerUsername,// 绑定科技侠账号，科技侠账号,为空未绑定
					bean.getScienerUsername());
		if (bean.getIdnumber() != null)
			SPUtils.put(getActivity(), null, SPContants.IdNumBer,// 身份证号码
					bean.getIdnumber());
		if (bean.getWalletPassword() != null)
			SPUtils.put(getActivity(), null, SPContants.WalletPass,// 钱包支付密码
					bean.getWalletPassword());
		if (bean.getRealname() != null)
			SPUtils.put(getActivity(), null, SPContants.RealName,
					bean.getRealname());// 身份证号码
		if (bean.getBaichuanUid() != null)
			SPUtils.put(getActivity(), null, SPContants.BaichuanUID,
					bean.getBaichuanUid());// 阿里百川即时通讯
		// 的uid

		if (bean.getWalletPassword() != null) {// 支付密码
			if (bean.getWalletPassword().equals("")) {
				SPUtils.put(getActivity(), null, SPContants.IS_ZHIFU_PASS,
						false);
			} else {
				SPUtils.put(getActivity(), null, SPContants.IS_ZHIFU_PASS, true);
			}
		} else {
			SPUtils.put(getActivity(), null, SPContants.IS_ZHIFU_PASS, false);
		}
	}
}
